package org.tinygroup.menuframe.fileresolver;

import com.thoughtworks.xstream.XStream;
import org.tinygroup.fileresolver.impl.AbstractFileProcessor;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.menuframe.config.UserConfigs;
import org.tinygroup.menuframe.manager.UserAuthManager;
import org.tinygroup.vfs.FileObject;
import org.tinygroup.xstream.XStreamFactory;

import java.io.InputStream;

/**
 * Created by wangwy11342 on 2016/10/18.
 */
public class ManageUserConfigLoaderFileProcessor extends AbstractFileProcessor {
    private static final String USER_LOAD_FILE_NAME = "manage.users.xml";
    private static final String USER_XSTEAM_PACKAGE_NAME = "manage_user";

    private UserAuthManager userAuthManager;

    public UserAuthManager getUserAuthManager() {
        return userAuthManager;
    }

    public void setUserAuthManager(UserAuthManager userAuthManager) {
        this.userAuthManager = userAuthManager;
    }

    protected boolean checkMatch(FileObject fileObject) {
        return fileObject.getFileName().equals(USER_LOAD_FILE_NAME);
    }

    public void process() {
        XStream stream = XStreamFactory
                .getXStream(USER_XSTEAM_PACKAGE_NAME);
        LOGGER.logMessage(LogLevel.INFO, "用户管理配置文件加载开始");
        for (FileObject fileObject : fileObjects) {
            LOGGER.logMessage(LogLevel.INFO, "找到用户管理配置文件:[{}]",
                    fileObject.getAbsolutePath());
            InputStream inputStream = fileObject.getInputStream();
            UserConfigs configs = (UserConfigs) stream
                    .fromXML(inputStream);
            try {
                inputStream.close();
            } catch (Exception e) {
                LOGGER.errorMessage("关闭文件流时出错,文件路径:{}", e, fileObject.getAbsolutePath());
            }
            userAuthManager.load(configs.getUserList());
        }
        // manager.load();
        LOGGER.logMessage(LogLevel.INFO, "用户管理配置文件加载结束");
    }
}
